package com.ehotting.eaf.core;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/***
 *
 * @Description: 日期操作类
 * @author 何绍亲
 * @date 2014-8-25 上午10:40:40
 *
 */
public class DateHelper {

	private static Logger logger = LoggerFactory.getLogger(DateHelper.class);

	public static String STD_FORMAT = "yyyy-MM-dd HH:mm:ss";


	public static String toStringYYYYMMDD(Date date) {
		return toString(date, "yyyyMMdd");
	}

	public static String toStringYYYYMMDDHHmmss(Date date) {
		return toString(date, "yyyyMMddHHmmss");
	}


	public static String toStringHHmmss(Date date) {
		return toString(date, "HHmmss");
	}

	public static String toString(Date date) {
		if (date == null)
			return null;

		return getFormat(null).format(date);
	}

	public static String toString(Date date, String format) {
		if (date == null)
			return null;

		return getFormat(format).format(date);
	}

	public static Date toDateyyyyMMddHHmmss(String date) {
		if (date == null)
			return null;

		return toDate(date, "yyyyMMddHHmmss");
	}

	public static Date toSeparatorDateyyyyMMddHHmmss(String date) {
		if (date == null)
			return null;

		return toDate(date, STD_FORMAT);
	}

	public static String toSeparatorStringyyyyMMddHHmmss(Date date) {
		if (date == null)
			return null;
		return getFormat(STD_FORMAT).format(date);
	}

	public static String toDate(String date) {
		if (date == null)
			return null;

		return getFormat(null).format(date);
	}


	public static Date toDate(String date, String format) {
		if (date == null)
			return null;

		try {
			return getFormat(format).parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}


	public static Date toDate000000(Date date, String format) {
		if (date == null)
			return null;
		String t = DateHelper.toString(date, format);
		return  toDate(t,format);
	}

	public static Date toDate235959(Date date, String format) {
		if (date == null)
			return null;
		String t = DateHelper.toString(date, format)+" 23:59:59";
		return  toDate(t,format+" HH:mm:ss");
	}

	public static Date toDate000000000(Date date, String format) {
		if (date == null)
			return null;
		String t = DateHelper.toString(date, format)+" 00:00:00:000";
		return  toDate(t,format+" HH:mm:ss:SSS");
	}

	public static Date toDate235959999(Date date, String format) {
		if (date == null)
			return null;
		String t = DateHelper.toString(date, format)+" 23:59:59:999";
		return  toDate(t,format+" HH:mm:ss:SSS");
	}

	public static Date toDate235959999(String date, String format) {
		if (date == null)
			return null;
		return  DateHelper.toDate(date+" 23:59:59:999",format+" HH:mm:ss:SSS");
	}

	public static Date toDate235959(String date, String format) {
		if (date == null)
			return null;
		return  DateHelper.toDate(date+" 23:59:59",format+" HH:mm:ss");
	}


	public static Date toNextDate000000(Date date, String format) {
		if (date == null)
			return null;

		Date newdate = new Date();

		newdate.setTime(date.getTime()+24*60*60*1000);

		String t = DateHelper.toString(newdate, format);

		return  toDate(t,format);
	}

	/**
	 * 获取当前时间的java.sql.Timestamp对象
	 *
	 * @return
	 */
	public static Timestamp getCurSqlTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}


	private static SimpleDateFormat getFormat(String format) {
		if (format == null) {
			format = "yyyy-MM-dd";
		}
		return new SimpleDateFormat(format);
	}

	/***
	 * 是否正确的日期格式
	 *
	 * @param date
	 * @param format
	 * @return
	 */
	public static boolean isDateFormat(String date, String format) {
		if (date == null)
			return false;

		try {
			getFormat(format).parse(date);
			return true;
		} catch (ParseException e) {
			logger.error("isDateFormat[date:" + date + ",format=" + format + "]", e);
			return false;
		}
	}

	/** 时间格式转换yyyy-MM-dd HH:mm:ss **/
	public static String dateConvert(String date) {
		if (date == null)
			return "";
		String formatDate = "";
		if (date.length() >= 8) {
			formatDate = date.substring(0, 4) + "-" + date.substring(4, 6)
					+ "-" + date.substring(6, 8) + " ";
		}
		if (date.length() >= 14) {
			formatDate += date.substring(8, 10) + ":" + date.substring(10, 12)
					+ ":" + date.substring(12, 14);
		}
		return formatDate;
	}

	public static int diffDay(Date date1, Date date2) {

		return (int) Math.abs(date1.getTime() / (1000) - date2.getTime() / (1000)) / (60 * 60 * 24);
	}

	/***
	 * 当时时间加天数据
	 *
	 * @param date
	 * @param i
	 * @return
	 */
	public static Date addMonth(Date date, int i) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 1);
		return cal.getTime();
	}

	/***
	 * 当时之前的 offset(月)的第一天
	 *
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date minusMonth(Date date, int offset) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.MONTH,-offset);
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMinimum(Calendar.DAY_OF_MONTH));
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		return ca.getTime();
	}

	/***
	 * 当时之前的 offset(年)的第一天
	 *
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date minusYear(Date date, int offset) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.MONTH,-(offset*12));

		ca.set(Calendar.DAY_OF_YEAR, ca.getActualMinimum(Calendar.DAY_OF_YEAR));
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);

		return ca.getTime();
	}

	public static Date minusSecond(Date date, long offset) {
		return addSecond( date, 0 - offset);
	}

	/***
	 * 当时时间加秒
	 *
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date addSecond(Date date, long offset) {
		Date d = new Date();
		long dateTime = 1000;
		d.setTime((date.getTime() / dateTime + offset) * dateTime);
		return d;
	}

	/***
	 * 当时时间加天数据
	 *
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date addDay(Date date, int offset) {
		Date d = new Date();
		long dateTime = 1000;
		d.setTime((date.getTime() / dateTime + offset * 60 * 60 * 24) * dateTime);
		return d;
	}

	/***
	 * 当天时间减去天数
	 *
	 * @param date
	 * @param i
	 * @return
	 */
	public static Date minusDay(Date date, int i) {
		return addDay(date, 0 - i);
	}


	/** 时间比较 **/
	public static boolean before(String a, String b) {
		Date d1 = toDateyyyyMMddHHmmss(a);
		Date d2 = toDateyyyyMMddHHmmss(b);
		if (d1 == null || d2 == null)
			return false;
		return d1.before(d2);
	}

	public static Date getYearFirstDate(){
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_YEAR, ca.getActualMinimum(Calendar.DAY_OF_YEAR));
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);

		return  ca.getTime();
	}

	public static Date getYearLastDate(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR));
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return  cal.getTime();
	}



	public static Date getMonthLastDate(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return  cal.getTime();
	}

	public static Date getMonthFirstDate(){
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMinimum(Calendar.DAY_OF_MONTH));
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);

		return  ca.getTime();
	}

	public static Date getWeekLastDate(){
		Calendar cal = Calendar.getInstance();

		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 6); // Sunday
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return  cal.getTime();
	}

	public static Date getWeekFirstDate(){
		Calendar cal = Calendar.getInstance();

		//cal.setFirstDayOfWeek(Calendar.MONDAY);
		if(cal.get(Calendar.WEEK_OF_MONTH)==1){
			System.out.println("_______________________");
			cal.set(Calendar.DAY_OF_MONTH, 1);
		}else if(cal.get(Calendar.DAY_OF_WEEK)==1){
			cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-7);
		}else{
			cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		}
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return  cal.getTime();
	}


	//最近多少天（不包括当天），返回日期的List
	public static List<String> lastDay(Date date,String format,int offset){
		List<String> days = new ArrayList<>();
		if(offset <= 0)
			return days;
		for(int i= offset; i>=0;i--){
			days.add(toString(DateHelper.minusDay(date, i),format));
		}
		return days;
	}

	//最近多少天（不包括当天），返回日期的List
	public static List<String> lastMonth(Date date,String format,int offset){
		List<String> months = new ArrayList<>();
		if(offset <= 0)
			return months;
		for(int i= offset; i>=0;i--){
			months.add(toString(DateHelper.minusMonth(date, i),format));
		}
		return months;
	}


	//最近多少天（不包括当天），返回日期的List
	public static List<String> lastYear(Date date,String format,int offset){
		List<String> years = new ArrayList<>();
		if(offset <= 0)
			return years;
		for(int i= offset; i>=0;i--){
			years.add(toString(DateHelper.minusYear(date, i),format));
		}
		return years;
	}

	public static void main(String[] args) {
//		Date d1 = DateHelper.toDate("20150503230000", "yyyyMMddHHmmss");
//		Date d2 = DateHelper.toDate("20150504000000", "yyyyMMddHHmmss");
//		System.out.println(DateHelper.diffDay(d1, d2));


//		Date d1 = DateHelper.toDate("20150503", "yyyyMMdd");
//		String d2 = DateHelper.toStringYYYYMMDDHHmmss(d1);
//		System.out.println("d2:"+d2);
//
//		System.out.println(DateHelper.toStringYYYYMMDD(DateHelper.getMonthFirstDate()));
//		System.out.println(DateHelper.toStringYYYYMMDD(DateHelper.getMonthLastDate()));
//		System.out.println(DateHelper.toStringYYYYMMDD(DateHelper.getWeekFirstDate()));
//		System.out.println(DateHelper.toStringYYYYMMDD(DateHelper.getWeekLastDate()));

//		System.out.println(DateHelper.toStringYYYYMMDDHHmmss(DateHelper.getYearFirstDate()));
//		System.out.println(DateHelper.toStringYYYYMMDDHHmmss(DateHelper.getYearLastDate()));
//		System.out.println(DateHelper.toStringYYYYMMDDHHmmss(DateHelper.minusYear(new Date(),2)));

//		logger.info("{}",DateHelper.lastMonth(new Date(),"yyyy-MM-dd",10));

		System.out.println(DateHelper.toString(DateHelper.toDate000000000(new Date(),"yyyyMMdd"),"yyyy-MM-dd hh:mm:ss:SSS"));
		System.out.println(DateHelper.toString(DateHelper.toDate235959999(new Date(),"yyyyMMdd"),"yyyy-MM-dd hh:mm:ss:SSS"));




	}

}
